/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: qingyang
 * @LastEditTime: 2022-05-19 14:30:11
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getGwpList } from '@/apis/system/config/gwp'
import type { GetGwpListParams } from '@/apis/system/config/model/gwpModel'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'

export const useGwpList = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetGwpListParams>({
    gwpName: null
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: 'GWP版本名称',
      field: 'gwpName',
      type: 'input'
    }
  ])

  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    border: true,
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: GetGwpListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getGwpList(params)
  }

  // 处理参数
  const handleParams = (): GetGwpListParams => {
    // 参数
    let params: GetGwpListParams = {
      gwpName: searchForm.gwpName
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: 'GWP版本',
      field: 'gwpName',
      minWidth: '170',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: 'GWP值',
      field: 'gwpValue',
      children: [
        {
          title: 'CO₂气体',
          field: 'coGwp',
          width: '120',
          align: 'right',
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: 'CH₄气体',
          field: 'chGwp',
          width: '120',
          align: 'right',
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: 'N₂O气体',
          field: 'noGwp',
          width: '120',
          align: 'right',
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: 'NF₃气体',
          field: 'nfGwp',
          width: '120',
          align: 'right',
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: 'HFCs&PFCs气体',
          field: 'hfcs',
          slot: 'hfcsSlot',
          width: '140',
          align: 'right',
          columnSettingConfig: {
            enabled: true
          }
        }
      ]
    },
    {
      key: 'op',
      title: '操作',
      field: 'op',
      slot: 'op',
      width: '120',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
